<template>
  <!-- 通联认证签约 -->
  <div>
    <van-popup
      v-model="show"
      round
      position="bottom"
      :style="{ height:'90%' }"
      closeable
    >
      <div class="tl-Qualification-container">
        <div class="login-title">修改绑定手机(银行卡验证)</div>
        <div class="login-user">
          <div class="login-line">
            <div class="login-name">持卡人姓名</div>
            <div class="login-val">
              <input type="text" name="name" placeholder="请输入姓名" v-model="formData.name" />
            </div>
          </div>
          <div class="login-line">
            <div class="login-name">持卡人身份证号</div>
            <div class="login-val">
              <input type="text" name="identity_no" placeholder="请输入身份证号" v-model="formData.identity_no" />
            </div>
          </div>
          <div class="login-line">
            <div class="login-name">银行卡号</div>
            <div class="login-val">
              <input type="number" name="cardNo" placeholder="请输入银行卡号" v-model="formData.card_no" />
            </div>
          </div>
          <div class="login-line">
            <div class="login-name">预留手机号</div>
            <div class="login-val">
              <input type="number" name="bankPhone" placeholder="请输预留手机号" v-model="formData.bank_phone" />
            </div>
          </div>
          <div class="login-line login-line-verify">
            <div class="login-name">验证码</div>
            <div class="login-val login-yan">
              <input
                class="msg-verify-input"
                type="text"
                name="code"
                placeholder="请输入验证码"
                id="vcode"
                v-model="formData.verification_code"
              />
              <a class="msg-verify-btn"
                 :class="verifyAtive?'active':''"
                 href="javascript:;"
                 @click="refreshmsgVerify">{{msgVerifyTxt}}</a>
            </div>
          </div>
        </div>
        <div class="btn-box">
          <div class="submit-btn red-gra-btn" @click="clickSubmit">确定</div>
        </div>
      </div>
    </van-popup>
  </div>

</template>

<script>
import { validLength, validateMobile } from '@/utils/validate'
import { tlGetMember, bankCardChangeBindPhone, verifyBankCardChangeBindPhone } from '@/api/user/member/memberApi'

export default {
  data() {
    return {
      msgVerifyTxt: '获取验证码',
      agree: false,
      phone: '',
      formData: {
        phone: '',
        verification_code: '',
        name: '',
        identity_type: 1, // 实名认证证件类型：1身份证
        identity_no: '', // 实名认证证件号
        card_no: '',
        bank_phone: ''
      },
      verifyAtive: false,
      show: false,
      tranceNum: ''
    }
  },
  created() {

  },
  methods: {
    handleShow(values) {
      // console.log(values)
      this.sendCodeParams = values
      this.show = true
      this.init()
    },
    init() {
      const addType = this.$route.query.addType

      if (addType && addType == 1) {
        document.title = '添加新银行卡'
      }

      this.$loadingWrap.show()
      setTimeout(() => {
        this.$loadingWrap.close()
      }, 50000)

      tlGetMember().then(res => {
        this.$loadingWrap.close()
        if (res.status == 1) {
          const resData = res.data
          this.phone = resData.phone
          this.formData.phone = resData.phone
          this.formData.verification_code = resData.verification_code || ''
          this.formData.name = resData.name
          this.formData.identity_no = resData.identity_no
          if (addType && addType == 1) {
            this.formData.card_no = ''
            this.formData.bank_phone = ''
          } else {
            this.formData.card_no = resData.tlfz_bind_bank[0] ? resData.tlfz_bind_bank[0].card_no : ''
            this.formData.bank_phone = resData.tlfz_bind_bank[0] ? resData.tlfz_bind_bank[0].bank_phone : resData.bank_phone
          }
        } else {
          this.$Error(res.msg)
        }
      })
    },
    // 获取验证码倒计时
    setTimeoutVerify() {
      let count = 60
      this.msgVerifyTxt = count + '秒后重新获取'
      const timer = setInterval(() => {
        this.msgVerifyTxt = count + '秒后重新获取'
        if (count <= 0) {
          this.msgVerifyTxt = '获取验证码'
          clearInterval(timer)
        }
        count--
      }, 1000)
    },
    // 获取短信验证码
    refreshmsgVerify() {
      if (this.msgVerifyTxt == '获取验证码') {
        if (validateMobile(this.formData.bank_phone)) {
          // veri_code_type 验证码类型 9=>绑定手机号 6=>解绑手机号
          const data = {
            name: this.formData.name,
            identity_type: this.formData.identity_type,
            identity_no: this.formData.identity_no,
            card_no: this.formData.card_no,
            bank_name: this.formData.bank_name,
            bank_phone: this.formData.bank_phone
          }
          this.$loadingWrap.show()
          bankCardChangeBindPhone(data).then(res => {
            this.$loadingWrap.close()
            if (res.status == 1) {
              this.setTimeoutVerify()
              this.$Error('发送成功')
              this.tranceNum = res.data.tranceNum
            } else {
              this.$Error(res.msg)
            }
          })
        } else {
          this.$Error('请输入正确的手机号')
        }
      }
    },
    // 确定签约
    clickSubmit() {
      if (!validLength(this.formData.name)) {
        this.$Error('请输入的持卡人姓名')
        return false
      } else if (!validLength(this.formData.identity_no)) {
        this.$Error('请输入的持卡人身份证号')
        return false
      } else if (!validLength(this.formData.card_no)) {
        this.$Error('请输入的银行卡号')
        return false
      } else if (!validateMobile(this.formData.bank_phone)) {
        this.$Error('请输入正确的预留手机号')
        return false
      }
      this.$loadingWrap.show()
      setTimeout(() => {
        this.$loadingWrap.close()
      }, 50000)
      const data = {
        code: this.formData.verification_code,
        tranceNum: this.tranceNum
      }
      verifyBankCardChangeBindPhone(data).then(res => {
        this.$loadingWrap.close()
        if (res.status == 1) {
          this.$Error(res.msg)
          this.show = false
          this.$emit('tlSubmit', true)
        } else {
          this.$Error(res.msg)
        }
      })
    }
  },
  watch: {
    'formData.phone'(val) {
      if (validateMobile(val)) {
        this.verifyAtive = true
      } else {
        this.verifyAtive = false
      }
    }
  }
}
</script>

<style lang="scss">
@import "src/styles/mixin";
.tl-Qualification-container {
  padding:0 24px;
  @include placeColor(#999);
  input:disabled {
    background: transparent;
  }
  .login-title{
    padding:30px 4px 20px;
    font-size:30px;
    line-height: 30px;
    color:black;
    text-align: center;
  }
  .login-user {
    background: #fff;
    width: 100%;
    overflow: hidden;
    border-radius:20px;
  }
  .login-line {
    display: flex;
    width: 100%;
    font-size: 32px;
    border-bottom: 1px solid #ededed;
    padding: 0 30px;
    box-sizing: border-box;
    &:last-child{
      border-bottom:0;
    }
    .login-name {
      width: 40%;
      line-height: 96px;
      font-size: 28px;
      color: #333333;
    }
    .login-val {
      width: 60%;
      position: relative;
      text-align:right;
      input {
        display: inline-block;
        font-size: 26px;
        color: #666666;
        border: 0;
        text-align: right;
        padding: 20px 0;
        height: 100px;
        background: transparent;
        &.birthday-input {
          padding-right: 50px;
        }
      }
      .icon_calendar {
        font-size: 36px;
        position: absolute;
        right: 0;
        top: 50%;
        transform: translateY(-50%);
      }
    }
    &.login-line-verify {
      .login-val{
        display: flex;
        justify-content: space-between;
        align-items: center;
      }
      .msg-verify-input {
        width: 50%;
        font-size: 26px;
        color: #999999;
      }
      .login-yan{
        width: 95%;
      }
      .msg-verify-btn {
        display: inline-block;
        // width: 150px;
        height: 54px;
        padding:0 20px;
        line-height: 54px;
        border-radius: 54px;
        text-align: center;
        font-size: 24px;
        color: #9A9A9A;
        border:2px solid #9A9A9A;
        &.active{
          padding: 0 30px;
          color:#F30C23;
          border-color:#F30C23;
        }
      }
    }
  }
  .login-user-style2{
    .login-line{
      .login-name{
        width:24%;
      }
      .login-val{
        width:76%;
        text-align:left;
        input{
          text-align:left;
        }
      }
    }
  }
  .application-txt{
    display: flex;
    justify-content: center;
    margin-top:34px;
    .application-name{
      color:#F11227;
    }
  }
  .btn-box {
    padding: 40px 0;
    font-size: 32px;
    text-align: center;
    .submit-btn {
      margin-bottom: 20px;
      border-radius: 92px;
      line-height: 92px;
      color: #fff;
      box-sizing: border-box;
    }
  }
}
</style>
